<!-- 编辑弹窗 -->
<template>
  <el-dialog
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :destroy-on-close="true"
    :lock-scroll="false"
    :title="isUpdate ? '为[' + form.username + ']设置CPA报警' : '设置CPA报警'"
    :visible="visible"
    custom-class="ele-dialog-form"
    width="526px"
    @update:visible="updateVisible"
  >
    <el-form ref="form" :model="form" label-width="142px">
      <el-form-item label="报警接受手机号:">
        <el-input
          v-model="form.cpa.mobileValue"
          placeholder="请输入手机号, 多个手机号请用英文逗号分开."
          type="text"
        ></el-input>
      </el-form-item>

      <el-form-item label="账户余额低于元:">
        <el-input-number
          v-model="form.cpa.balanceValue"
          :max="999999"
          :min="0"
          class="ele-fluid ele-text-left"
          controls-position="right"
        ></el-input-number>
      </el-form-item>

      <el-form-item label="账户回传成本报警值:">
        <el-input-number
          v-model="form.cpa.userValue"
          :max="999999"
          :min="0"
          class="ele-fluid ele-text-left"
          controls-position="right"
        ></el-input-number>
      </el-form-item>

      <el-form-item label="创意回传成本报警值:">
        <el-input-number
          v-model="form.cpa.creativeValue"
          :max="999999"
          :min="0"
          class="ele-fluid ele-text-left"
          controls-position="right"
        ></el-input-number>
      </el-form-item>

      <el-form-item label="计划回传成本报警值:">
        <el-input-number
          v-model="form.cpa.planValue"
          :max="999999"
          :min="0"
          class="ele-fluid ele-text-left"
          controls-position="right"
        ></el-input-number>
      </el-form-item>

      <el-form-item label="单元回传成本报警值:">
        <el-input-number
          v-model="form.cpa.unitValue"
          :max="999999"
          :min="0"
          class="ele-fluid ele-text-left"
          controls-position="right"
        ></el-input-number>
      </el-form-item>
    </el-form>

    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button :loading="loading" type="primary" @click="save"
        >确定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: "semAccountCpa",
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object,
  },
  data() {
    return {
      // 表单数据
      form: Object.assign(
        {
          cpa: {
            mobileValue: "",
          },
        },
        this.data
      ),
      // 提交状态
      loading: false,
      // 是否为编辑数据
      isUpdate: false,
    };
  },
  watch: {
    data() {
      if (Object.keys(this.data).length > 0) {
        this.isUpdate = true;
        this.form = Object.assign(
          {
            cpa: {
              mobileValue: "",
            },
          },
          this.data
        );
      } else {
        this.isUpdate = false;
      }
    },
  },
  methods: {
    /* 确定编辑 */
    save() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http
            .put("/sem/updateCpa", this.form)
            .then((res) => {
              this.loading = false;
              if (res.data.code === 0) {
                this.$message({ type: "success", message: res.data.msg });
                this.updateVisible(false);
                this.$emit("done");
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch((e) => {
              this.loading = false;
              this.$message.error(e.message);
            });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit("update:visible", value);
    },
  },
};
</script>

<style scoped></style>
